﻿// <copyright file="QldFederalResultsByElectorate2004FileDefinition.cs" company="QUT">
//     Copyright (c) Ninety Nine Percent. All rights reserved.
// </copyright>

using System;
using System.IO;
using FileHelpers;
using FileHelpers.DataLink;

namespace Qut.GenerateKml.Data.FileDefinitions
{
    /// <summary>
    /// Represents the file structure of the Qld_FederalResults by Electorate-2004.xls data file.
    /// </summary>
    [DelimitedRecord("|")]
    [IgnoreLast(1)]
    public class QldFederalResultsByElectorate2004FileDefinition
    {
        /// <summary>
        /// Gets or sets the Division
        /// </summary>
        public string Division { get; set; }

        /// <summary>
        /// Gets or sets the State
        /// </summary>
        public string State { get; set; }

        /// <summary>
        /// Gets or sets the Australian Labor Party
        /// </summary>
        public double ALP { get; set; }

        /// <summary>
        /// Gets or sets the Liberal
        /// </summary>
        public double LP { get; set; }

        /// <summary>
        /// Gets or sets The Nationals
        /// </summary>
        public double NP { get; set; }

        /// <summary>
        /// Gets or sets the Democrats
        /// </summary>
        public double DEM { get; set; }

        /// <summary>
        /// Gets or sets The Greens
        /// </summary>
        public double GRN { get; set; }

        /// <summary>
        /// Gets or sets the Other
        /// </summary>
        public double OTH { get; set; }

        /// <summary>
        /// Gets or sets the Two-Party Preferred Liberal
        /// </summary>
        public double TPPLP { get; set; }

        /// <summary>
        /// Gets or sets the Two-Party Preferred Australian Labor Party
        /// </summary>
        public double TPPALP { get; set; }

        /// <summary>
        /// Loads the specified data file from the file path.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="db">The data context.</param>
        public static void Load(string filePath, SociologicalDataClassesDataContext db)
        {
            var provider = new ExcelStorage(typeof(QldFederalResultsByElectorate2004FileDefinition), filePath, 3, 1);
            var results = provider.ExtractRecords() as QldFederalResultsByElectorate2004FileDefinition[];
            db.TruncateQldFederalResultsByElectorate2004();
            foreach (var item in results)
            {
                if (item.Division == "National Total")
                {
                    continue;
                }

                if (item.State == "QLD")
                {
                    var record = new QldFederalResultsByElectorate2004();
                    record.ALP = item.ALP;
                    record.DEM = item.DEM;
                    record.Division = item.Division;
                    record.GRN = item.GRN;
                    record.LP = item.LP;
                    record.NP = item.NP;
                    record.OTH = item.OTH;
                    record.State = item.State;
                    record.TPPALP = item.TPPALP;
                    record.TPPLP = item.TPPLP;
                    db.QldFederalResultsByElectorate2004s.InsertOnSubmit(record);
                }
            }

            db.SubmitChanges();
        }
    }
}
